package cn.esbot.wx.mp.config;

import cn.esbot.wx.mp.client.EsbotTemplate;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.core.StringRedisTemplate;

@Configuration
@AllArgsConstructor
@EnableConfigurationProperties(value = EsbotProperties.class)
public class EsbotConfiguration {
    private EsbotProperties esbotProperties;
    private StringRedisTemplate redisTemplate;

    @Bean
    public EsbotTemplate esbotTemplate() {
        EsbotTemplate esbotTemplate = new EsbotTemplate();
        if (StringUtils.isAnyBlank(esbotProperties.getAppId(), esbotProperties.getAppKey(),
                esbotProperties.getRobotUrl())) {
            throw new RuntimeException("配置出错，请重新配置！");
        }
        esbotTemplate.setRedisTemplate(redisTemplate);
        esbotTemplate.setAppId(esbotProperties.getAppId());
        esbotTemplate.setAppKey(esbotProperties.getAppKey());
        esbotTemplate.setRobotUrl(esbotProperties.getRobotUrl());
        return esbotTemplate;
    }
}
